package org.demo3;



import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //class类中用于获取成员方法法
        //1.先获取class字节码文件的对象
        Class clazz = Class.forName("org.demo3.student");

        //2.获取成员方法的对象
        //(1)
        Method[] methods = clazz.getMethods();//包含父类中所有的公共方法
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println();

        //(2)
        Method[] declaredMethods = clazz.getDeclaredMethods();//不包含父类的
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }

        System.out.println();

        //(3)
        Method method = clazz.getDeclaredMethod("eat", String.class);
        System.out.println(method);

        System.out.println();

        //(4)
        int modifiers = clazz.getModifiers();//获取方法的修饰符
        System.out.println(modifiers);
        String name = method.getName();//获取方法的名字
        System.out.println(name);
        Parameter[] parameters = method.getParameters();//获取方法的参数
        for (Parameter parameter : parameters) {
            System.out.println(parameter);
        }
        Class<?>[] exceptionTypes = method.getExceptionTypes();//获取方法抛出的异常
        for (Class<?> exceptionType : exceptionTypes) {
            System.out.println(exceptionType);
        }

        student student = new student();
        method.setAccessible(true);
        method.invoke(student,"蛋糕");//方法运行,参数一：方法的调用者，参数二：调用方法传递的参数

    }
}